const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

module.exports = {
  mode: "development",
  entry: {
    app: "./src/index.js"
  },
  devtool: "inline-source-map",
  output: {
    filename: "[name].[chunkhash].bundle.js",
    path: path.resolve(__dirname, "dist")
  },
  devServer: {
    contentBase: path.resolve(__dirname, "dist"),
    port: "8081",
    proxy: {
      "/api": {
        target: "http://localhost:3001",
        pathRewrite: {
          "^/api": ""
        }
      },
      "/resource": {
        target: "http://localhost:3002",
        pathRewrite: {
          "^/resource": ""
        }
      }
    }
  },
  plugins: [
    new HtmlWebpackPlugin({
      title: "Output Management",
      template: "./index.html"
    }),
    new MiniCssExtractPlugin({
      filename: "[name]-[contenthash].css"
    })
  ],
  module: {
    rules: [
      {
        test: /\.m?js$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader"
        }
      },
      {
        test: /\.css$/,
        use: [MiniCssExtractPlugin.loader, "css-loader"]
      },
      {
        test: /\.(png|svg|jpg|gif|webp)$/,
        use: [
          {
            loader: "url-loader",
            options: {
              limit: 10000,
              name: "assets/[name]_[hash:10].[ext]"
            }
          }
        ]
      }
    ]
  }
};
